﻿//+--------------------------------------------------------------------------------------------------
//     
//Description: DataVirtualHardDiskViewModel
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.ComponentModel;
using VmroleAuthor.Models;

namespace VmroleAuthor.ViewModels
{
    public class DataVirtualHardDiskViewModel : ViewModelBase, IDataErrorInfo, ICleanupParamsAndPayload
    {
        private readonly DataVirtualHardDisk dataVirtualHardDisk;     
        private readonly VmRoleResourceDefinitionViewModel vmRoleResourceDefinitionViewModel;
        
        private DiskParameterizableProperty dataVirtualHardDiskImage;      
        private IntParameterizableProperty lun;

        public static class PropertyNames
        {
            public const string DataVirtualHardDiskImage = "DataVirtualHardDiskImage";
            public const string Lun = "Lun";          
        }       

        /// <summary>
        /// Constructor
        /// </summary>
        public DataVirtualHardDiskViewModel(DataVirtualHardDisk dataVirtualHardDisk, VmRoleResourceDefinitionViewModel resourceDefinitionViewModel)
        {
            this.dataVirtualHardDisk = dataVirtualHardDisk;
            this.vmRoleResourceDefinitionViewModel = resourceDefinitionViewModel;           
        }

        public DataVirtualHardDisk DataVirtualHardDisk
        {
            get { return this.dataVirtualHardDisk; }            
        }

        public List<string> AvailableParameters
        {
            get { return this.vmRoleResourceDefinitionViewModel.ResourceDefinitionParametersViewModel.AvailableParameters; }
        }

        public DiskParameterizableProperty DataVirtualHardDiskImage
        {
            get
            {
                if (this.dataVirtualHardDiskImage == null)
                {
                    Func<string> paramValueGetter = () =>
                        {
                            if (string.IsNullOrEmpty(this.dataVirtualHardDisk.DataVirtualHardDiskImage))
                            {
                                return "Enter new Data Virtual Hard Disk Image";
                            }
                            return this.dataVirtualHardDisk.DataVirtualHardDiskImage;
                        };
                    Action<string> paramValueSetter = delegate(string value) { this.dataVirtualHardDisk.DataVirtualHardDiskImage = value; };
                    Func<string> autoParamNameGetter = () => PropertyNames.DataVirtualHardDiskImage;
                    ViewDefParamInfo viewDefParamInfo = new ViewDefParamInfo
                    {
                        ViewDefParameterType = ViewDefParameterType.DataVirtualHardDisk,                       
                        DefaultEnglishLabelText = "Data virtual hard disk"
                    };

                    this.dataVirtualHardDiskImage = new DiskParameterizableProperty(
                        PropertyNames.DataVirtualHardDiskImage,
                        paramValueGetter,
                        paramValueSetter,
                        autoParamNameGetter,
                        ParameterType.String,
                        "Data Virtual Hard Disk",
                        this.vmRoleResourceDefinitionViewModel.ResourceDefinitionParametersViewModel,
                        true,                      
                        viewDefParamInfo);
                }
                return this.dataVirtualHardDiskImage;
            }
        }


        public IntParameterizableProperty Lun
        {
            get
            {
                if (this.lun == null)
                {
                    Func<string> paramValueGetter = () =>
                        {
                            if (string.IsNullOrEmpty(this.dataVirtualHardDisk.Lun))
                            {
                                return "Enter Lun (integer)";
                            }
                            return this.dataVirtualHardDisk.Lun;
                        };
                    Action<string> paramValueSetter = delegate(string value) { this.dataVirtualHardDisk.Lun = value; };
                    Func<string> autoParamHintGetter = () => PropertyNames.DataVirtualHardDiskImage + PropertyNames.Lun;
                    ViewDefParamInfo viewDefParamInfo = new ViewDefParamInfo
                    {
                        ViewDefParameterType = ViewDefParameterType.Number,
                        DefaultInitialValue = "0"
                    };

                    this.lun = new IntParameterizableProperty(
                        PropertyNames.Lun,
                        paramValueGetter,
                        paramValueSetter,
                        autoParamHintGetter,
                         ParameterType.Int,
                        "Data disk lun",
                        this.vmRoleResourceDefinitionViewModel.ResourceDefinitionParametersViewModel,
                        true,
                        viewDefParamInfo
                        );
                }
                return this.lun;
            }           
        }

        public void Cleanup()
        {          
            this.Lun.Cleanup();
            this.DataVirtualHardDiskImage.Cleanup();
        }

        public string this[string propertyName]
        {
            get
            {  
                switch (propertyName)
                {
                    case PropertyNames.DataVirtualHardDiskImage:
                        if (string.IsNullOrEmpty(this.dataVirtualHardDisk.DataVirtualHardDiskImage))
                        {
                            return "Data Virtual Hard Disk Image must be specified";
                        }
                        break;
                    case PropertyNames.Lun:                        
                        if (!this.Lun.IsParameterized)
                        {
                            int lunNumber;
                            if (Int32.TryParse(this.lun.PropertyValue, out lunNumber))
                            {
                                if (lunNumber < 0)
                                {
                                    return "Cannot specify negative value";
                                }                               
                            }
                            else
                            {
                                return "Invalid intger for lun";
                            }                            
                        }
                        break;
                }
                return null;
            }
        }

        public string Error { get { return null; } }
    }
}
